package com.android.questions.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.WindowManager;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import com.android.base.util.ActivityClickUtil;
import com.android.base.util.DisplayUtil;
import com.android.questions.R;
import com.android.questions.config.AppConfig;
import com.android.questions.databinding.DialogDailyWithdrawalBinding;
import com.dq.ad.AdManager;

/**
 * FileName: DialogDailyWithdrawal
 * Author: admin
 * Date: 2021/11/9 20:15
 * Description:
 */
public class DialogDailyWithdrawal extends Dialog {

    private DialogDailyWithdrawalBinding withdrawalBinding;
    private final Activity activity;
    private OnClickLister clickLister;
    private final int taskProgress = AppConfig.getDayAnswerNumber();

    public DialogDailyWithdrawal(Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        withdrawalBinding = DialogDailyWithdrawalBinding.inflate(activity.getLayoutInflater());
        setContentView(withdrawalBinding.getRoot());

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);

        getWindow().setWindowAnimations(R.style.dialog_answer_correct);

        setCanceledOnTouchOutside(false);
        setCancelable(false);
        initView();
    }

    private void initView() {
        ActivityClickUtil.getInstance().clear();
        String title = "答对<font color='#FD001A'>" + AppConfig.GEAR_WITHDRAWAL[0] + "、" + AppConfig.GEAR_WITHDRAWAL[1] + "</font>道题<br>即可将存钱罐内现金取出";
        withdrawalBinding.tvTitle.setText(Html.fromHtml(title));

        withdrawalBinding.tvCoin.setText(AppConfig.getTotalPiggy() + "");

        int extractTimes = AppConfig.getExtractTimes();

        if (extractTimes < 1) {
            // 第一轮
            if (taskProgress < AppConfig.GEAR_WITHDRAWAL[0]) {
                withdrawalBinding.imgReceive.setImageResource(R.drawable.icon_receive_disabled);
                withdrawalBinding.imgReceive.setEnabled(false);
                withdrawalBinding.tvMsg.setTextColor(ContextCompat.getColor(activity, R.color.Color_000000));
                withdrawalBinding.tvMsg.setText(Html.fromHtml("当前还差<font color='#FD001A'>" + (AppConfig.GEAR_WITHDRAWAL[0] - taskProgress) + "</font>题可提取"));
            } else {
                withdrawalBinding.tvMsg.setTextColor(ContextCompat.getColor(activity, R.color.Color_EC656D));
                withdrawalBinding.tvMsg.setText("可提取");
            }
        } else if (extractTimes < 2) {
            // 第二轮
            if (taskProgress < AppConfig.GEAR_WITHDRAWAL[1]) {
                withdrawalBinding.imgReceive.setImageResource(R.drawable.icon_receive_disabled);
                withdrawalBinding.imgReceive.setEnabled(false);
                withdrawalBinding.tvMsg.setTextColor(ContextCompat.getColor(activity, R.color.Color_000000));
                withdrawalBinding.tvMsg.setText(Html.fromHtml("当前还差<font color='#FD001A'>" + (AppConfig.GEAR_WITHDRAWAL[1] - taskProgress) + "</font>题可提取"));
            } else {
                withdrawalBinding.tvMsg.setTextColor(ContextCompat.getColor(activity, R.color.Color_EC656D));
                withdrawalBinding.tvMsg.setText("可提取");
            }
        } else {
            // 今日无法再提取
            withdrawalBinding.imgReceive.setImageResource(R.drawable.icon_today_over);
            withdrawalBinding.imgReceive.setEnabled(false);
            withdrawalBinding.tvMsg.setTextColor(ContextCompat.getColor(activity, R.color.Color_000000));
            withdrawalBinding.tvMsg.setText("明日可继续提取");
        }

        withdrawalBinding.imgReceive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ActivityClickUtil.getInstance().isFastClick()) {
                    //防止用户快速点击
                    return;
                }
                dismiss();
                if (clickLister != null) {
                    if (extractTimes < 1) {
                        clickLister.onWithdrawal(1000);
                    } else if (extractTimes < 2) {
                        clickLister.onWithdrawal(2000);
                    }
                }
            }
        });
        withdrawalBinding.ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ActivityClickUtil.getInstance().isFastClick()) {
                    //防止用户快速点击
                    return;
                }
                dismiss();
            }
        });

        AdManager.getInstance().showNativeAD(activity, withdrawalBinding.nativeContainer, DisplayUtil.getScreenWidthDp(activity) - 48, 0, AdManager.NATIVE_AD_TYPE_UP_IMAGE, false, new AdManager.OnNativeVideoADListener() {
            @Override
            public void onSuccess() {
                withdrawalBinding.nativeContainer.setBackgroundColor(activity.getResources().getColor(R.color.Color_FFFFFF));
            }

            @Override
            public void onClosed() {
                withdrawalBinding.nativeContainer.setBackgroundColor(activity.getResources().getColor(android.R.color.transparent));
            }

            @Override
            public void onError() {
            }

            @Override
            public void onClick() {
            }

            @Override
            public void onShow(View view) {

            }
        });
    }

    public DialogDailyWithdrawal setClickLister(OnClickLister onClickLister) {
        this.clickLister = onClickLister;
        return this;
    }

    public interface OnClickLister {
        void onWithdrawal(double coin);
    }
}
